package io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

public class Demo6 {

	public static void main(String[] args) throws IOException {
		trasfer("F:/pic/11.txt");
	}
	
	//转换流
	public static void trasfer(String path) throws IOException{
		File file = new File(path);
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(new FileInputStream(file),"utf-8"));
	    String line = null;
		while(null != (line = reader.readLine())){
			System.out.println(line);
		}
	}
	
	//缓冲流
	public static void read(String path){
		File file = new File(path);
		if(!file.exists() || file.isDirectory()){
			System.out.println("文件读取失败");
		}
		//选择流
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
//			char[] buffer = new char[10];
//			int len = 0;
//			while(-1 != (len = reader.read(buffer))){
//				System.out.println(new String(buffer,0,len));
//			}
			String line = null;
			while(null != (line = reader.readLine())){
				System.out.println(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != reader){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/*
	 * 在节点流之上加入缓冲流，建议这样写
	 * reader同理
	 */
	public static void read(){
		//建立文件联系
		String path = "F:\\pic\\3.jpg";
		File file = new File(path);
		//选择流
		InputStream is = null;
		try {
			is = new BufferedInputStream(new FileInputStream(file));
			//操作不断读取，缓冲数组
		    byte[] buffer = new byte[10];
		    //实际读取大小
		    int len = 0;
		    
		    while(-1 != (len = is.read(buffer))){
		    	//输出，字节数组转字符串
		    	String info = new String(buffer,0,len);
		    	System.out.println(info);
		    }
		    
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != is){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
